export const openWindow = (url, opts = {}) => {
  const { target = "_blank", ...others } = opts || {};
  window.open(
    url,
    target,
    Object.entries(others)
      .reduce((preValue, curValue) => {
        const [key, value] = curValue;
        return [...preValue, `${key}=${value}`];
      }, [])
      .join(",")
  );
};

export const regexUrl = new RegExp(
  "^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$",
  "i"
);

export default null;

export function thumb(url) {
  if (!url) {
    return "https://upload.jxntv.cn/2025/0814/1755178824856.jpg";
  }
  // 判断是否为绝对路径
  if (url.indexOf("http") !== -1) {
    return url;
  } else {
    return "https://res.yun.jxntv.cn" + url;
  }
}

/**
 * 格式化时长（秒）为 小时:分:秒 或 分:秒 格式
 * @param {number} seconds - 时长（秒）
 * @returns {string} 格式化后的时长字符串
 */
export function formatDuration(seconds) {
  if (!seconds || isNaN(seconds)) return "0'00''";

  let hours = Math.floor(seconds / 3600);
  let minutes = Math.floor((seconds % 3600) / 60);
  let secs = Math.floor(seconds % 60);

  // 格式化显示
  return hours > 0 ? `${hours}:${minutes}:${secs}` : `${minutes}:${secs}`;
}

/**
 * 格式化文件大小（KB）为 KB、MB 或 GB 格式
 * @param {number} size - 文件大小（KB）
 * @returns {string} 格式化后的文件大小字符串
 */
export function formatFileSize(size) {
  if (!size || isNaN(size)) return "0 KB";

  // 1KB = 1024B
  // 1MB = 1024KB
  // 1GB = 1024MB
  if (size < 1024) {
    return `${size} KB`;
  } else if (size < 1024 * 1024) {
    return `${(size / 1024).toFixed(2)} MB`;
  } else {
    return `${(size / (1024 * 1024)).toFixed(2)} GB`;
  }
}
